require 'Common/define'
require 'Logic/Config'
require 'Common/functions'

UIRecord = {}
HelperFunc.SetUIEnv(UIRecord)
local this = UIRecord
function UIRecord.Init(data)
    this.battle_data = data
    local name, packageName = getUIName(PanelResNames.UIRecord)
    panelMgr:Push(name, packageName, this.onCreate, Config.useLocalUI)
end

function UIRecord.onCreate(obj)
    if not this.hasInit then
        this.hasInit = true
        bgClose = obj:GetChild("btnCloseReward")
        itemGrid = obj:GetChild("itemGrid")
        itemGrid.itemRenderer = this.UpdateListItem
        this.addEvent()
    end
    this.initData()
end

function UIRecord.initData()
    itemGrid.numItems = #this.battle_data
end

function UIRecord.UpdateListItem(index, obj)
    local data = this.battle_data[index + 1]
    local ctrl_win = obj:GetController("ifWin")
    local txt_my_name = obj:GetChild("nameMy")
    local txt_enemy_name = obj:GetChild("nameEnemy")
    local head_my = obj:GetChild("headMy")
    local head_enemy = obj:GetChild("headEnemy")
    local my_hero_list = obj:GetChild("heroMy")
    local enemy_hero_list = obj:GetChild("heroEnemy")
    local txt_num = obj:GetChild("num")
    local btn_play = obj:GetChild("n5")
    btn_play.onClick:Clear()

    if data ~= nil then
        local num_str = "第一场"
        if index == 1 then
            num_str = "第二场"
        elseif index == 2 then
            num_str = "第三场"
        end
        txt_num.text = num_str
        if data.win == 1 then
            ctrl_win:SetSelectedIndex(0)
        else
            ctrl_win:SetSelectedIndex(1)
        end
        btn_play.onClick:Add(function()
            if data.version == GetFightVersion() then
                FightManager.cross_team_battle_data = this.battle_data and clone(this.battle_data) or {}
                FightManager.cross_team_battle_record = this.battle_data and clone(this.battle_data) or {}
                local win_count = 0
                local defender_score = nil
                local attacker_score = nil
                for k, v in ipairs(this.battle_data) do
                    if v.win == 1 then
                        win_count = win_count + 1
                    end
                    if defender_score == nil then
                        defender_score = v.defender and tonumber(tostring(v.defender.score))
                    end
                    if attacker_score == nil then
                        attacker_score = v.attacker and tonumber(tostring(v.attacker.score))
                    end
                end
                FightManager.cross_team_battle_data["defener_score"] = defender_score
                FightManager.cross_team_battle_data["attacker_score"] = attacker_score
                FightManager.cross_team_battle_data["win_count"] = win_count
                BattlePlaybackManager.ClearMultiFightData()
                BattlePlaybackManager.AddMultiFightData(data.fightId, data.win, EnumConst.FightTypeConst.ARENA, data, nil, -1, -1)
                BattlePlaybackManager.SetFormationType(EnumConst.FormationType.TeamCrossArenaAtk)
                BattlePlaybackManager.GotoNextFight(true)
            else
                UISysTips.AddMsg(getLanguage("ErrorCode_220005"));
                return ;
            end
        end)

        local attacker = data.attacker
        local defender = data.defender
        txt_my_name.text = attacker.playerName or "--"
        txt_enemy_name.text = defender.playerName or "--"

        head_my:GetChild("head"):GetChild("HeadIcon").url = logicMgr.SkinManager.GetHeroIconBySkinId(attacker.pic)
        head_my:GetChild("frame").url = EnumConst.AtlasPath.ICONAtlas .. attacker.picFrame

        head_enemy:GetChild("head"):GetChild("HeadIcon").url = logicMgr.SkinManager.GetHeroIconBySkinId(defender.pic)
        head_enemy:GetChild("frame").url = EnumConst.AtlasPath.ICONAtlas .. defender.picFrame

        my_hero_list.itemRenderer = this.renderHeroItem
        enemy_hero_list.itemRenderer = this.renderHeroItem2
        this.my_hero_list_data = attacker.members or {}
        my_hero_list.numItems = #this.my_hero_list_data
        this.enemy_hero_list_data = defender.members or {}
        enemy_hero_list.numItems = #this.enemy_hero_list_data
    end
end

function UIRecord.renderHeroItem(index, obj)
    local data = this.my_hero_list_data[index + 1]
    this.showItemInfo(obj, data)
end
function UIRecord.renderHeroItem2(index, obj)
    local data = this.enemy_hero_list_data[index + 1]
    this.showItemInfo(obj, data)
end
function UIRecord.showItemInfo(obj, item_info)
    local quality = obj:GetChild("quality")
    local qualityBG = obj:GetChild("qualityBG")
    local icon = obj:GetChild("icon")
    local lv = obj:GetChild("level")
    local lvbg = obj:GetChild("n29")
    local choose = obj:GetChild("maskChoose")
    local unActive = obj:GetChild("unActive")
    local circleCtrl = obj:GetController("Circle")
    local comIcon = obj:GetChild("Com_icon")
    local levelShowCtrl = obj:GetController("isShowLevel")--0显示等级以及底框 1不显示
    quality.visible = false
    qualityBG.visible = false
    choose.visible = false
    unActive.visible = false
    icon.visible = false
    if item_info then
        if item_info.star ~= nil then
            item_info.quality = logicMgr.HeroManager.starToQuality(item_info["star"]);
        end
        local hero_item = G.dataTable["t_hero"][tostring(item_info.partnerModelId)]
        if levelShowCtrl ~= nil then
            if item_info.level ~= nil and item_info.level > 0 then
                levelShowCtrl:SetSelectedIndex(0)
                lv.visible = true
                lvbg.visible = true
                local level, lvType = logicMgr.HeroManager.getHeroShowLevelByPartnerId(item_info.partnerId)
                lv.text = string.TextColor(string.format(getLanguage("Level"), level), logicMgr.HeroManager.lvColors[lvType])
            else
                levelShowCtrl:SetSelectedIndex(1)
            end
        end
        if hero_item ~= nil then
            if circleCtrl and circleCtrl.selectedIndex == 1 then
                comIcon.visible = true
                comIcon:GetChild("icon").url = "ui://ICONAtlas/" .. hero_item.f_HeroIcon
            else
                icon.visible = true
                icon:GetChild("icon").url = "ui://ICONAtlas/" .. hero_item.f_HeroIcon
            end
            icon.visible = true
            icon:GetChild("icon").url = "ui://ICONAtlas/" .. hero_item.f_HeroIcon
            if item_info.quality == nil or item_info.quality == 0 then
                item_info.quality = 1
            elseif item_info.quality > #Config.IconQuality then
                item_info.quality = #Config.IconQuality
            end
            quality.visible = true
            qualityBG.visible = true
            quality.url = "ui://ICONAtlas/" .. Config.IconFrame[item_info.quality]
            qualityBG.url = "ui://ICONAtlas/" .. Config.IconQuality[item_info.quality]
        end
    end
end

function UIRecord.addEvent()
    bgClose.onClick:Add(this.onClickBack)
end

function UIRecord.removeEvent()
    bgClose.onClick:Clear()
end

function UIRecord.LocalListenEvent()
end

function UIRecord.LocalCloseEvent()
end

function UIRecord.OnDestroy()
    this.removeEvent()
    this.hasInit = nil
end

function UIRecord.OnEnable()
    this.LocalListenEvent();
end

function UIRecord.OnDisable()
    this.LocalCloseEvent();
end

function UIRecord.onClickBack()
    closeUI(PanelNames.UIRecord)
end

return UIRecord
